<!--
Copyright 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="../ct-test-list.html">

<link rel="import" href="../../model/ct-step-failure.html">

<script>
(function () {

var assert = chai.assert;

var kExampleTests = [
  new CTStepFailure("foo_step", "plugins/gesture-events-scrolled.html", {}, 177164, 177165),
  new CTStepFailure("foo_step", "plugins/transformed-events.html", {}, 177164, 177165),
  new CTStepFailure("foo_step", "plugins/gesture-events.html", {}, 177164, 177165),
];

var kExampleTestsChromium = [
  new CTStepFailure("browser_tests", "SomeTest.SubTest1", {}, 177164, 177165),
  new CTStepFailure("browser_tests", "SomeTest.SubTest2", {}, 177164, 177165),
];

var kExampleTestsBlink = [
  new CTStepFailure("webkit_tests", "fast/events/foo.html", {}, 177164, 177165),
  new CTStepFailure("webkit_tests", "fast/events/bar.html", {}, 177164, 177165),
];

function createTestList(tests) {
  var list = document.createElement('ct-test-list');
  list.tests = tests;
  return list;
}

describe('ct-test-list', function() {
  var list;

  describe('tests failed', function() {
    before(function(done) {
      list = createTestList(kExampleTests);
      setTimeout(done);
    });

    it('should show all tests', function() {
      var tests = list.shadowRoot.querySelectorAll('a');
      assert.equal(tests.length, 3);
      assert.equal(tests[0].href, 'http://test-results.appspot.com/dashboards/flakiness_dashboard.html#tests=plugins%2Fgesture-events-scrolled.html&testType=foo_step');
      assert.equal(tests[1].href, 'http://test-results.appspot.com/dashboards/flakiness_dashboard.html#tests=plugins%2Ftransformed-events.html&testType=foo_step');
      assert.equal(tests[2].href, 'http://test-results.appspot.com/dashboards/flakiness_dashboard.html#tests=plugins%2Fgesture-events.html&testType=foo_step');
    });
  });

  describe('group of tests failed', function() {
    afterEach(function() {
      var tests = list.shadowRoot.querySelectorAll('a');
      assert.lengthOf(tests, 0);
      var icons = list.shadowRoot.querySelectorAll('paper-icon-button');
      assert.lengthOf(icons, 1);
    });

    it('should group chromium tests', function(done) {
      list = createTestList(kExampleTestsChromium);
      setTimeout(done);
    });

    it('should group blink tests', function(done) {
      list = createTestList(kExampleTestsBlink);
      setTimeout(done);
    });
  });

  describe('many tests failed', function() {
    var manyTests = [];
    before(function() {
      for (var i = 0; i < 12; i++) {
        manyTests.push(new CTStepFailure("base_unittests", "SomeTest{1}.SubTest".assign(i), {}));
      }
    });
    it('should clamp tests to 10 when there are 12 or more', function(done) {
      list = createTestList(manyTests);
      setTimeout(function() {
        var links = list.shadowRoot.querySelectorAll('a');
        assert.lengthOf(links, 10);
        done();
      });
    });
    it('should show all tests when clicked', function(done) {
      list = createTestList(manyTests);
      setTimeout(function() {
        var buttons = list.shadowRoot.querySelectorAll('#outerExpand');
        assert.lengthOf(buttons, 1);
        buttons[0].dispatchEvent(new CustomEvent('click'));
        setTimeout(function() {
          var links = list.shadowRoot.querySelectorAll('a');
          assert.lengthOf(links, 12);
          var buttons = list.shadowRoot.querySelectorAll('#outerExpand');
          assert.lengthOf(buttons, 0);
          done();
        });
      });
    });
    it('should show all tests when no steps have >10 tests each', function(done) {
      var sixTestsEach = [];
      for (var step of ["step1", "step2"]) {
        for (var i = 0; i < 6; i++) {
          sixTestsEach.push(new CTStepFailure(step, "SomeTest{1}.SubTest".assign(i), {}));
        }
      }
      list = createTestList(sixTestsEach);
      setTimeout(function() {
        var links = list.shadowRoot.querySelectorAll('a');
        assert.lengthOf(links, 12);
        done();
      });
    });
  });
});

})();
</script>
